package com.lqd.mall.system.service.impl;

import com.lqd.mall.basis.exception.ExceptionCast;
import com.lqd.mall.basis.utils.StringUtils;
import com.lqd.mall.interaction.basis.vo.TableDataInfoVo;
import com.lqd.mall.interaction.system.SysDictDataEntity;
import com.lqd.mall.jpa.service.impl.BaseServiceImpl;
import com.lqd.mall.system.repository.SysDictDataRepository;
import com.lqd.mall.system.service.ISysDictDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @author 零起点
 * @create 2022-03-20 22:01
 */
@Service
public class SysDictDataServiceImpl
        extends BaseServiceImpl<SysDictDataRepository, SysDictDataEntity, Long>
        implements ISysDictDataService {

    @Resource
    SysDictDataRepository sysDictDataRepository;

    @Override
    public List<SysDictDataEntity> findSysDictData(String dictType) {
        SysDictDataEntity entity = new SysDictDataEntity();
        entity.setDictType(dictType);
        TableDataInfoVo<SysDictDataEntity> infoVo = this.list(entity);
        return infoVo.getRows();
    }

    @Override
    public Optional<Integer> upateDictData(String oldValue, String newValue) {
        if (StringUtils.isAllEmpty(oldValue, newValue)) {
            ExceptionCast.cast("更新数据不能为空");
        }
        return Optional.of(sysDictDataRepository.upateDictType(oldValue, newValue));
    }

    @Override
    public Optional<Integer> save(SysDictDataEntity entity) {
        if (StringUtils.isEmpty(entity.getDictType())) {
            ExceptionCast.cast("字典类型不能为空");
        }
        if (StringUtils.isEmpty(entity.getDictLabel())) {
            ExceptionCast.cast("字典标签不能为空");
        }
        SysDictDataEntity save = sysDictDataRepository.save(entity);
        return Optional.of(1);
    }

    @Override
    public Optional<Integer> deleteSysDictData(Long dictCode) {
        SysDictDataEntity entity = new SysDictDataEntity();
        entity.setDictCode(dictCode);
        entity.setIsDefault("1");
        return this.updateEntity(entity, "dict_code");

    }


}
